package com.heaven.customerServer.customerController;

import com.heaven.customerServer.customerController.vo.login.LoginRes;
import com.heaven.customerServer.customerController.vo.message.ChatHistoryRes;
import com.heaven.customerServer.customerController.vo.message.ChatTarget;
import com.heaven.service.MessageService;
import com.heaven.utils.R;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("heaven-server/message")
public class MessageController {
    @Resource
    private MessageService messageService;
    @Resource
    private LoginController loginController;

    /**
     * 获取聊天对象列表
     */
    @GetMapping("/auth/list-chat-targets")
    public R getChatTargets() {
        LoginRes.UserSession session = loginController.getUserSession();
        List<ChatTarget> ans = messageService.getChatTargets(session.getUserId());
        return R.okData(ans);
    }

    /**
     * 搜索聊天对象的基本信息（一般与该用户之前没有聊天消息）
     * @param targetUserId 目标用户id
     * @return 基本信息
     */
    @GetMapping("/auth/get-userinfo")
    public R getUserInfo(@RequestParam("userId") String targetUserId) {
        LoginRes.UserSession session = loginController.getUserSession();
        ChatTarget ans = messageService.getUserInfo(session.getUserId(), targetUserId);
        return R.okData(ans);
    }

    /**
     * 获取与聊天历史记录
     * @param targetUserId 目标用户id
     * @param pageSize 页大小 默认10
     * @param pageNum 页码
     */
    @GetMapping("/auth/get-chat-history")
    public R getChatHistory(@RequestParam("targetUserId") String targetUserId
                            , @RequestParam("pageSize") int pageSize
                            , @RequestParam("pageNum") int pageNum) {
        if(pageSize <= 0 || pageNum < 1) {
            return R.error("页大小或页码不能为0或负数");
        }
        LoginRes.UserSession session = loginController.getUserSession();
        ChatHistoryRes ans = messageService.getChatHistory(session.getUserId(), targetUserId, pageSize, pageNum);
        return R.okData(ans);
    }
}
